package controllers;

import play.data.Form;
import play.mvc.Controller;
import play.mvc.Result;
import views.html.login;

/**
 * Created with IntelliJ IDEA.
 * User: bjornth
 * Date: 21.2.2013
 * Time: 14:31
 * To change this template use File | Settings | File Templates.
 */
public class AccountController extends Controller {

    public static class Login {

        public String email;
        public String password;

        public String validate() {
            //if (User.authenticate(kennitala, notandanr) == null) {
            //    return "Invalid user or password";
            //}
            return null;
        }
    }

    final static Form<Login> userForm = form(Login.class);
    public static Result login() {
        return ok(login.render(userForm));
    }

    public static Result logout(){
        session().clear();
        flash("success", "Thou hast been logged out!");
        return redirect(routes.AccountController.login());
    }

    public static Result authenticate(){
        Form<Login> filledForm = userForm.bindFromRequest();

        if (filledForm.hasErrors()) {
            return badRequest(login.render(filledForm));
        } else {
            session().clear();
            session("email", filledForm.get().email);
            return redirect(routes.Application.index());

        }
    }


}
